package org.jeecg.modules.flower.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.flower.entity.*;
import org.jeecg.modules.flower.service.*;
import org.jeecg.modules.flower.vo.TFlowerTagInMain2Page2;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.def.TemplateExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecgframework.poi.excel.view.JeecgTemplateExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: 蔬菜成本和统计
 * @Author: jeecg-boot
 * @Date: 2022-06-29
 * @Version: V1.0
 */
@Api(tags = "蔬菜成本和统计")
@RestController
@RequestMapping("/flower/flowerCost")
@Slf4j
public class FlowerCostController extends JeecgController<FlowerCost, IFlowerCostService> {
    @Autowired
    private IFlowerCostService flowerCostService;
    @Autowired
    private ITFlowerTagIn2Service flowerTagIn2Service;
    @Autowired
    private IFlowerStorehouseService flowerStorehouseService;
    @Autowired
    private IFlowerPlantingGreenhouseService greenhouseService;

    @Autowired
    private IFlowerOutorderService flowerOutorderService;
    @Autowired
    private IFlowerOutorderinfoService flowerOutorderInfoService;
    @Autowired
    private IFlowerPreService flowerPreService;

    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private ISysUserService sysUserService;

    @Value("${yth.tempExcelinHZ}")
    private String tempExcelin;

    /**
     * 查询蔬菜出库的成本
     *
     * @param storehouse
     * @param cost
     * @param sDate
     * @param eDate
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "蔬菜收入和统计-列表")
    @ApiOperation(value = "蔬菜收入和统计-分页列表查询", notes = "蔬菜收入和统计-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            @RequestParam(name = "storehouse", defaultValue = "") String storehouse,
            @RequestParam(name = "cost", defaultValue = "") String cost,
            @RequestParam(name = "sDate", defaultValue = "") String sDate,
            @RequestParam(name = "eDate", defaultValue = "") String eDate,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Page<FlowerCost> page = new Page<>(pageNo, pageSize);
        IPage<FlowerCost> pageList = flowerCostService.queryCosts(page, storehouse, cost, sDate, eDate);
        return Result.ok(pageList);
    }


    /**
     * 蔬菜收入和统计  导出excel
     *
     * @param request
     * @param storehouse
     * @param cost
     * @param sDate
     * @param eDate
     * @return
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request,
                                  @RequestParam(name = "storehouse", defaultValue = "") String storehouse,
                                  @RequestParam(name = "cost", defaultValue = "") String cost,
                                  @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                  @RequestParam(name = "eDate", defaultValue = "") String eDate) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Page<FlowerCost> page = new Page<>(1, 99999999);
        IPage<FlowerCost> exportList = flowerCostService.queryCosts(page, storehouse, cost, sDate, eDate);
        List<FlowerCost> list = exportList.getRecords();
        for (FlowerCost flowerCost : list) {
            String cost1 = flowerCost.getCost();
            String storehouse1 = flowerCost.getStorehouse();

            QueryWrapper<FlowerStorehouse> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("code", storehouse1);
            FlowerStorehouse house = flowerStorehouseService.getOne(queryWrapper2);
            flowerCost.setStorehouse(house.getName());

            String costname = sysDictService.queryDictTextByKey("ch_cost", cost1);
            flowerCost.setCost(costname);
        }
        String title = "蔬菜成本和统计(" + sDate + "-" + eDate + ")";
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "title"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, FlowerCost.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title, "导出人:" + sysUser.getRealname(), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }

    @AutoLog(value = "蔬菜明细表查询统计===盘点 上期结余。本期入库，出库， 本期结余 ")
    @ApiOperation(value = "蔬菜明细表查询统计===盘点 上期结余。本期入库，出库， 本期结余 ", notes = "蔬菜明细表查询统计===盘点 上期结余。本期入库，出库， 本期结余 ")
    @GetMapping(value = "/flowerAccount")
    public Result<?> flowerAccount(FlowerAcont flowerAcont,
                                   @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                   @RequestParam(name = "eDate", defaultValue = "") String eDate,
                                   HttpServletRequest req) {

        JSONObject jsonObject = flowerCostService.flowerAccount(flowerAcont.getStorehouse(), flowerAcont.getCost(), sDate, eDate);

        return Result.ok(jsonObject);
    }

    @RequestMapping(value = "/exportXlsflowerAccount")
    public ModelAndView exportXlsflowerAccount(HttpServletRequest request, FlowerAcont flowerAcont,
                                               @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                               @RequestParam(name = "eDate", defaultValue = "") String eDate) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        JSONObject jsonObject = flowerCostService.flowerAccount(flowerAcont.getStorehouse(), flowerAcont.getCost(), sDate, eDate);

        // 过滤选中数据
//        String selections = request.getParameter("selections");
//        List<FlowerAcont> list = new ArrayList<FlowerAcont>();
//        if (oConvertUtils.isEmpty(selections)) {
//            list = pageList;
//        } else {
//            List<String> selectionList = Arrays.asList(selections.split(","));
//            list = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
//        }

        String title = "成品花库存明细表(" + sDate + "-" + eDate + ")";
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, FlowerAcont.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title, "导出人:" + sysUser.getRealname(), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, jsonObject.get("list"));
        return mv;
    }

    @AutoLog(value = "结算单汇总表")
    @ApiOperation(value = "结算单汇总表", notes = "结算单汇总表")
    @GetMapping(value = "/orderCountList")
    public Result<?> orderCountList(FlowerOutorder flowerOutorder,
                                    @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                    @RequestParam(name = "eDate", defaultValue = "") String eDate,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                    HttpServletRequest req) {
        QueryWrapper<FlowerOutorder> queryWrapper = QueryGenerator.initQueryWrapper(flowerOutorder, req.getParameterMap());
        Page<FlowerOrderCountList> page = new Page<>(pageNo, pageSize);
        IPage<FlowerOrderCountList> pageList = flowerCostService.orderCountList(page, queryWrapper, sDate, eDate);
        return Result.ok(pageList);
    }

    //结算单汇总表
    @RequestMapping(value = "/orderCountListExcel")
    public ModelAndView exportXlsflowerAccount(FlowerOutorder flowerOutorder,
                                               @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                               @RequestParam(name = "eDate", defaultValue = "") String eDate,
                                               HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        QueryWrapper<FlowerOutorder> queryWrapper = QueryGenerator.initQueryWrapper(flowerOutorder, req.getParameterMap());
        Page<FlowerOrderCountList> page = new Page<>(1, 99999999);
        IPage<FlowerOrderCountList> pageList = flowerCostService.orderCountList(page, queryWrapper, sDate, eDate);
        // 过滤选中数据
        String selections = req.getParameter("selections");
        List<FlowerOrderCountList> flowerOutorderList = new ArrayList<FlowerOrderCountList>();
        if (oConvertUtils.isEmpty(selections)) {
            flowerOutorderList = pageList.getRecords();
        } else {
            List<String> selectionList = Arrays.asList(selections.split(","));
            flowerOutorderList = pageList.getRecords().stream().filter(item -> selectionList.contains(item.getOrderNo())).collect(Collectors.toList());
        }


        String title = "结算单汇总表(" + sDate + "-" + eDate + ")";
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, FlowerOrderCountList.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title, "导出人:" + sysUser.getRealname(), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, flowerOutorderList);
        return mv;
    }


    @AutoLog(value = "预出库结算单汇总表")
    @ApiOperation(value = "预出库结算单汇总表", notes = "预出库结算单汇总表")
    @GetMapping(value = "/orderPreAccontlist")
    public Result<?> orderPreAccontlist(FlowerOrderPreCountList flowerOrderPreCountList,
                                        @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                        @RequestParam(name = "eDate", defaultValue = "") String eDate,
                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                        HttpServletRequest req) {
        QueryWrapper<FlowerOrderPreCountList> queryWrapper = QueryGenerator.initQueryWrapper(flowerOrderPreCountList, req.getParameterMap());
        Page<FlowerOrderPreCountList> page = new Page<>(pageNo, pageSize);
        IPage<FlowerOrderPreCountList> pageList = flowerCostService.orderPreAccontlist(page, queryWrapper, sDate, eDate);
        return Result.ok(pageList);
    }


    //预出库结算单汇总表
    @RequestMapping(value = "/orderPreAccontlistExcel")
    public ModelAndView orderPreAccontlistExcel(FlowerOrderPreCountList flowerOrderPreCountList,
                                                @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                                @RequestParam(name = "eDate", defaultValue = "") String eDate,
                                                HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        QueryWrapper<FlowerOrderPreCountList> queryWrapper = QueryGenerator.initQueryWrapper(flowerOrderPreCountList, req.getParameterMap());
        Page<FlowerOrderPreCountList> page = new Page<>(1, 99999999);

        IPage<FlowerOrderPreCountList> pageList = flowerCostService.orderPreAccontlist(page, queryWrapper, sDate, eDate);


        // 过滤选中数据
        String selections = req.getParameter("selections");
        List<FlowerOrderPreCountList> flowerOutorderList = new ArrayList<FlowerOrderPreCountList>();
        if (oConvertUtils.isEmpty(selections)) {
            flowerOutorderList = pageList.getRecords();
        } else {
            List<String> selectionList = Arrays.asList(selections.split(","));
            flowerOutorderList = pageList.getRecords().stream().filter(item -> selectionList.contains(item.getNo())).collect(Collectors.toList());
        }

        String title = "预出库结算单汇总表(" + sDate + "-" + eDate + ")";
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, FlowerOrderPreCountList.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title, "导出人:" + sysUser.getRealname(), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, flowerOutorderList);
        return mv;
    }


    @AutoLog(value = "入库汇总表")
    @ApiOperation(value = "入库汇总表", notes = "入库汇总表")
    @GetMapping(value = "/queryInListByDate")
    @PermissionData(pageComponent = "floweraccount/FlowerTagInListForExcel")
    public Result<?> queryInListByDate(TFlowerTagIn2 flowerTagIn,
                                       @RequestParam(name = "sDate", defaultValue = "") String sDate,
                                       @RequestParam(name = "eDate", defaultValue = "") String eDate,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {
        if (StringUtils.isBlank(eDate)) {
            eDate = sDate + " 23:59:59";
        }
        QueryWrapper<TFlowerTagIn2> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(flowerTagIn.getStorehouse())) {
            queryWrapper.eq("a.storehouse", flowerTagIn.getStorehouse());
        }
        QueryGenerator.installAuthMplus(queryWrapper, TFlowerTagIn2.class);

        Page<TFlowerTagIn2> page = new Page<>(pageNo, pageSize);
        IPage<TFlowerTagIn2> pageList = flowerCostService.queryInListByDate(page, queryWrapper, sDate, eDate);
        return Result.ok(pageList);
    }

    /**
     * 导出excel入库汇总单
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @descriptionauthor
     * @author gf
     * 2022-10-27 13:37
     * [sDate, eDate, req]
     */
    @RequestMapping(value = "/exportXlsForTemp")
    @ApiOperation(value = "导出excel入库单-导出excel入库单", notes = "导出excel入库单-导出excel入库单")
    @PermissionData(pageComponent = "floweraccount/FlowerTagInListForExcel")
    public ModelAndView exportXlsForTemp(TFlowerTagIn2 flowerTagInQuery, @RequestParam(name = "sDate") String sDate,
                                         @RequestParam(name = "eDate", defaultValue = "") String eDate,
                                         HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        if (StringUtils.isBlank(tempExcelin)) {
            throw new RuntimeException("还未配置入库单模板，请联系管理员配置入库单模板");
        }
        TemplateExportParams params = new TemplateExportParams(tempExcelin);//模板

        if (StringUtils.isBlank(eDate)) {
            eDate = sDate + " 23:59:59";
        }
        QueryWrapper<TFlowerTagIn2> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(flowerTagInQuery.getStorehouse())) {
            queryWrapper.eq("a.storehouse", flowerTagInQuery.getStorehouse());
        }
        QueryGenerator.installAuthMplus(queryWrapper, TFlowerTagIn2.class);
        Page<TFlowerTagIn2> page = new Page<>(1, 999999);
        IPage<TFlowerTagIn2> pageList = flowerCostService.queryInListByDate(page, queryWrapper, sDate, eDate);
        List<TFlowerTagIn2> list = pageList.getRecords();

        if (list.size() <= 0) {
            throw new RuntimeException("没有数据，不可导出");
        }
        TFlowerTagIn2 flowerTagIn = list.get(0);

        List<FlowerStorehouse> FlowerStorehouseList = flowerStorehouseService.list();
        List<FlowerPlantingGreenhouse> Greenhouselist1 = greenhouseService.list();
        List<DictModel> ch_cost = sysDictService.getDictItems("ch_cost");

        String no = "";

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sortTime", sDate);
        map.put("cost", "");
        map.put("no", sDate);
        SysUser user = sysUserService.getUserByName(flowerTagIn.getCreateBy());
        map.put("createBy", user.getRealname());

        List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
        BigDecimal countPrice = BigDecimal.ZERO;
        BigDecimal countNum = BigDecimal.ZERO;
        int i = 1;
        for (TFlowerTagIn2 flowerTagIn2 : list) {
            Map<String, Object> lm = new HashMap<String, Object>();
            lm.put("no", i);
            i++;
            lm.put("storehouse", getbase(FlowerStorehouseList, flowerTagIn2.getStorehouse()));
            lm.put("sourceGreenhouse", getGreenHouse(Greenhouselist1, flowerTagIn2.getSourceGreenhouse()));
            lm.put("variety", flowerTagIn2.getVariety());
            lm.put("varietyCode", flowerTagIn2.getVarietyCode());

            lm.put("packingSpecifications", flowerTagIn2.getPackingSpecifications());
            lm.put("flowerPoleLength", flowerTagIn2.getFlowerPoleLength());
            lm.put("number", flowerTagIn2.getTagNum());
            lm.put("beizhu", getCost(ch_cost, flowerTagIn2.getCost()));
            countNum = countNum.add(flowerTagIn2.getTagNum());//计算数量合计

            listMap.add(lm);
        }
        if (list.size() == 1) {
            Map<String, Object> lm = new HashMap<String, Object>();
            listMap.add(lm);
        }
        map.put("sumNumber", countNum);
        map.put("maplist", listMap);

        ModelAndView mv2 = new ModelAndView(new JeecgTemplateExcelView());
        mv2.addObject(TemplateExcelConstants.PARAMS, params);
        mv2.addObject(TemplateExcelConstants.MAP_DATA, map);
        return mv2;
    }

    private String getbase(List<FlowerStorehouse> list, String code) {
        String rest = "";
        for (FlowerStorehouse storehouse : list) {
            if (storehouse.getCode().equals(code)) {
                rest = storehouse.getName();
                break;
            }
        }
        return rest;
    }

    private String getGreenHouse(List<FlowerPlantingGreenhouse> list, String id) {
        String rest = "";
        for (FlowerPlantingGreenhouse storehouse : list) {
            if (storehouse.getId().equals(id)) {
                rest = storehouse.getName();
                break;
            }
        }
        return rest;
    }

    private String getCost(List<DictModel> list, String code) {
        String rest = "";
        for (DictModel ddd : list) {
            if (ddd.getValue().equals(code)) {
                rest = ddd.getText();
                break;
            }
        }
        return rest;
    }


    @AutoLog(value = "实物出库汇总报表---导出")
    @ApiOperation(value = "实物出库汇总报表---导出", notes = "实物出库汇总报表---导出")
    @GetMapping(value = "/queryOutListByDate")
    public Result<?> queryOutListByDate(
            @RequestParam(name = "storehouse", defaultValue = "") String storehouse,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "isPre", defaultValue = "") String isPre,
            @RequestParam(name = "sDate", defaultValue = "") String sDate,
            @RequestParam(name = "eDate", defaultValue = "") String eDate,
            HttpServletRequest req) {
        if (StringUtils.isBlank(eDate)) {
            eDate = sDate + " 23:59:59";
        }
        Page<FlowerOutorderHZ> page = new Page<>(pageNo, pageSize);
        IPage<FlowerOutorderHZ> flowerOutorderHZIPage = flowerCostService.queryOutListByDate(page, storehouse, isPre, sDate, eDate);
        return Result.ok(flowerOutorderHZIPage);
    }


    //实物出库汇总报表---导出
    @RequestMapping(value = "/queryOutListByDateExcel")
    public ModelAndView queryOutListByDateExcel(
            @RequestParam(name = "storehouse", defaultValue = "") String storehouse,
            @RequestParam(name = "isPre", defaultValue = "") String isPre,
            @RequestParam(name = "sDate", defaultValue = "") String sDate,
            @RequestParam(name = "eDate", defaultValue = "") String eDate,
            HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();


        if (StringUtils.isBlank(eDate)) {
            eDate = sDate + " 23:59:59";
        }
        Page<FlowerOutorderHZ> page = new Page<>(1, 99999999);
        IPage<FlowerOutorderHZ> pageList = flowerCostService.queryOutListByDate(page, storehouse, isPre, sDate, eDate);


//        // 过滤选中数据
//        String selections = req.getParameter("selections");
//        List<FlowerOrderCountList> flowerOutorderList = new ArrayList<FlowerOrderCountList>();
//        if (oConvertUtils.isEmpty(selections)) {
//            flowerOutorderList = pageList.getRecords();
//        } else {
//            List<String> selectionList = Arrays.asList(selections.split(","));
//            flowerOutorderList = pageList.getRecords().stream().filter(item -> selectionList.contains(item.getOrderNo())).collect(Collectors.toList());
//        }


        String title = "出库单汇总表导出(" + sDate + "-" + eDate + ")";
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, FlowerOutorderHZ.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title, "导出人:" + sysUser.getRealname(), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList.getRecords());
        return mv;
    }


    @AutoLog(value = "实物出库 日 报表")
    @ApiOperation(value = "实物出库 日 报表", notes = "实物出库 日 报表")
    @GetMapping(value = "/queryOutDayListByDate")
    public Result<?> queryOutDayListByDate(
            @RequestParam(name = "storehouse", defaultValue = "") String storehouse,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "isPre", defaultValue = "") String isPre,
            @RequestParam(name = "sDate", defaultValue = "") String sDate,
            @RequestParam(name = "eDate", defaultValue = "") String eDate,
            HttpServletRequest req) {
        if (StringUtils.isBlank(eDate)) {
            eDate = sDate + " 23:59:59";
        }
        Page<FlowerOutorderDayHZ> page = new Page<>(pageNo, pageSize);
        IPage<FlowerOutorderDayHZ> flowerOutorderHZIPage = flowerCostService.queryOutDayListByDate(page, storehouse, isPre, sDate, eDate);
        return Result.ok(flowerOutorderHZIPage);
    }


    //实物出库 日 报表---导出
    @RequestMapping(value = "/queryOutDayListByDateExcel")
    public ModelAndView queryOutDayListByDateExcel(
            @RequestParam(name = "storehouse", defaultValue = "") String storehouse,
            @RequestParam(name = "isPre", defaultValue = "") String isPre,
            @RequestParam(name = "sDate", defaultValue = "") String sDate,
            @RequestParam(name = "eDate", defaultValue = "") String eDate,
            HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        if (StringUtils.isBlank(eDate)) {
            eDate = sDate + " 23:59:59";
        }
        Page<FlowerOutorderDayHZ> page = new Page<>(1, 99999999);
        IPage<FlowerOutorderDayHZ> pageList = flowerCostService.queryOutDayListByDate(page, storehouse, isPre, sDate, eDate);

        String title = "出库单汇总表导出(" + sDate + "-" + eDate + ")";
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, FlowerOutorderDayHZ.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title, "导出人:" + sysUser.getRealname(), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList.getRecords());
        return mv;
    }


    @AutoLog(value = "销售系统大屏获取-销售数据")
    @ApiOperation(value = "销售系统大屏获取-销售数据", notes = "销售系统大屏获取-销售数据")
    @GetMapping(value = "/getSealInfos")
    public Result<?> getSealInfos() {
        List<FlowerOutorderHZ> sealInfos = flowerCostService.getSealInfos();

        long count = flowerOutorderService.count();
        long count2 = flowerPreService.count();
        JSONObject join = new JSONObject();
        join.put("orderNum", (count + count2));
        join.put("list", sealInfos);
        return Result.ok(join);
    }

    @AutoLog(value = "销售系统大屏获取-近3个月销售数据")
    @ApiOperation(value = "销售系统大屏获取-近3个月销售数据", notes = "销售系统大屏获取-近3个月销售数据")
    @GetMapping(value = "/getThreeMonthSealInfos")
    public Result<?> getThreeMonthSealInfos() {
        List<FlowerOutorderHZ> sealInfos = flowerCostService.getThreeMonthSealInfos();

        QueryWrapper<FlowerOutorder> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().apply("sureouttime>DATE_SUB(CURDATE(), INTERVAL 3 MONTH)");
        long count = flowerOutorderService.count(queryWrapper1);
        QueryWrapper<FlowerPre> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().apply("stock_out_time>DATE_SUB(CURDATE(), INTERVAL 3 MONTH)");
        long count2 = flowerPreService.count(queryWrapper2);
        JSONObject join = new JSONObject();
        join.put("orderNum", (count + count2));
        join.put("list", sealInfos);
        return Result.ok(join);
    }


    @AutoLog(value = "app-近7天入库")
    @ApiOperation(value = "app-近7天入库", notes = "app-近7天入库")
    @GetMapping(value = "/getIn7")
    public Result<?> getIn7() {

        QueryWrapper<TFlowerTagIn2> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("   DATE_FORMAT(sort_time,'%m-%d') as variety, sum(tag_num) as tag_num , packing_specifications");
        queryWrapper1.groupBy(" DATE_FORMAT(sort_time,'%m-%d') ");
        queryWrapper1.last("limit 7");
        List<TFlowerTagIn2> list = flowerTagIn2Service.list(queryWrapper1);
        for (TFlowerTagIn2 tFlowerTagIn2: list) {
            String packingSpecifications = tFlowerTagIn2.getPackingSpecifications();
            BigDecimal tagNum = tFlowerTagIn2.getTagNum();
            String newKGUQUzhong = packingSpecifications.replaceAll("[^\\d.]", "");
            if(StringUtils.isBlank(newKGUQUzhong)){
                newKGUQUzhong = "1";
            }
            tFlowerTagIn2.setTagNum( tagNum .multiply( new BigDecimal( newKGUQUzhong )) );
        }


        QueryWrapper<FlowerOutorderinfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("   DATE_FORMAT(create_Time,'%m-%d') as variety, sum(number) as number ,packing_specifications ");
        queryWrapper2.groupBy(" DATE_FORMAT(create_Time,'%m-%d') ");
        queryWrapper2.last("limit 7");
        List<FlowerOutorderinfo> list2 = flowerOutorderInfoService.list(queryWrapper2);

        for (FlowerOutorderinfo tFlowerTagIn2: list2) {
            String packingSpecifications = tFlowerTagIn2.getPackingSpecifications();
            BigDecimal tagNum = tFlowerTagIn2.getNumber();
            String newKGUQUzhong = packingSpecifications.replaceAll("[^\\d.]", "");
            if(StringUtils.isBlank(newKGUQUzhong)){
                newKGUQUzhong = "1";
            }
            tFlowerTagIn2.setNumber( tagNum .multiply( new BigDecimal( newKGUQUzhong )) );
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("list2", list2);
        return Result.ok(jsonObject);
    }

    @AutoLog(value = "app-品种分类")
    @ApiOperation(value = "app-品种分类", notes = "app-品种分类")
    @GetMapping(value = "/getInVers")
    public Result<?> getInVers() {

        QueryWrapper<TFlowerTagIn2> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select(" variety , sum(tag_num) as tag_num ");
        queryWrapper1.groupBy("variety");
        List<TFlowerTagIn2> list = flowerTagIn2Service.list(queryWrapper1);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        return Result.ok(jsonObject);
    }

}
